summaryrefslogtreecommitdiffstats
path: root/domain2name.c
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2022-01-04 00:33:23 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2022-01-04 00:33:23 +0100
commitf0f075abae47994279864fcdfe76c9e31b65bc25 (patch)
tree70b150f8edb0809186f2ab9303511576124ac407 /domain2name.c
downloaddnsfind-f0f075abae47994279864fcdfe76c9e31b65bc25.tar
dnsfind-f0f075abae47994279864fcdfe76c9e31b65bc25.tar.gz
dnsfind-f0f075abae47994279864fcdfe76c9e31b65bc25.tar.bz2
dnsfind-f0f075abae47994279864fcdfe76c9e31b65bc25.tar.lz
dnsfind-f0f075abae47994279864fcdfe76c9e31b65bc25.tar.xz
dnsfind-f0f075abae47994279864fcdfe76c9e31b65bc25.tar.zst
dnsfind-f0f075abae47994279864fcdfe76c9e31b65bc25.zip
Diffstat (limited to 'domain2name.c')
-rw-r--r--domain2name.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/domain2name.c b/domain2name.c
new file mode 100644
index 0000000..46fd2a3
--- /dev/null
+++ b/domain2name.c
@@ -0,0 +1,40 @@
+int domain2name_len (const char * s, int l) {
+ int r = 1; /* ending terminator */
+ int o = 0; /* label offset */
+ for (int i = 0; i < l; i++) {
+ if (s[i] == '.') {
+ if (!o) /* double period or starting period, label is empty */
+ break;
+ o = 0;
+ continue;
+ }
+ if (!o) /* label has started */
+ r++;
+ r++;
+ o++;
+ }
+ return r;
+}
+int domain2name (char * d, const char * s, int l) { /* l is length of s */
+ char * c = d; /* where to write the label size when done with label */
+ char * w = d;
+ int o = 0; /* label offset */
+ for (int i = 0; i <= l /* yes, we go one more ... */; i++) {
+ if (i == l /* ... here */ || s[i] == '.') { /* end of label or end of last label */
+ if (!o) /* double period or starting period, label is empty */
+ break;
+ if (o <= 63) /* max length of label (six bits) */
+ *c = o;
+ o = 0;
+ continue;
+ }
+ if (!o++) /* label has started */
+ c = w++; /* to be filled with length */
+ if (o <= 63)
+ *w++ = s[i];
+ if (o == 64) /* if this label is too long, instead of writing it, we silently cap */
+ *c = 63; /* it at 63 bytes */
+ }
+ *w++ = '\0'; /* luckily this makes domain2name kind of safe for handling as a string (: */
+ return w-d; /* we return number of bytes written */
+} /* d must be allocated for at least _len. */